package com.bytecat.algui.service;

import static android.os.Process.myPid;

import android.app.Service;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;

import com.bytecat.algui.ICommandService;
import com.jimo.api.ApiConstant;

public class CommandService extends Service {
    private static final String TAG = "emmmmmmmm";
    private ICommandService service;
    private IBinder ibinder;
    private final ServiceConnection connection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder binder) {
            ibinder =binder;
            Log.i(TAG, "宿主的ipc other进程 连接上了 宿主进程");
            service = ICommandService.Stub.asInterface(binder);
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            Log.i(TAG, "宿主的ipc other进程 断开了 宿主进程");
            ibinder =null;
            service =null;
        }
    };


    @Override
    public void onCreate() {
        super.onCreate();
        Log.i(TAG, "onCreate: CommandService");
        Intent intent = new Intent(ApiConstant.HostServiceName);
        intent.setPackage(getPackageName());
        bindService(intent, connection, Context.BIND_AUTO_CREATE);
    }


    @Override
    public void onDestroy() {
        super.onDestroy();
       try{
           unbindService(connection);
           service =null;
           ibinder =null;
       }catch (Exception ignore){}
    }
    private final ICommandService.Stub binder = new ICommandService.Stub() {
        @Override
        public void bindAidlToApp() {
            Log.i(TAG, "onCreate: CommandService");
            Intent intent = new Intent(ApiConstant.HostServiceName).setPackage(getPackageName());
            bindService(intent, connection, Context.BIND_AUTO_CREATE);
        }



        @Override
        public String test() throws RemoteException {
            try {
                return service.test();
            } catch (RemoteException e) {
                return "";
            }
        }

        @Override
        public boolean isAidlConnectApp()  {
            if(service==null||ibinder==null||!ibinder.pingBinder()){
                Log.i(TAG, "getPid: 宿主进程不存在");
                return false;
            }else {
                return true;
            }
        }

        @Override
        public void killSelf(boolean onlyMainProcess) throws RemoteException {
            try {
                service.killSelf(onlyMainProcess);
            } catch (RemoteException ignore) {
            }
            unbindService(connection);
            service=null;
            ibinder=null;
            if(!onlyMainProcess){
               android.os.Process.killProcess(myPid());
            }

        }

        @Override
        public int getPid()  {
            try {
                return service.getPid();
            } catch (RemoteException e) {
                return -1;
            }
        }

        @Override
        public int getAidlPid()  {
            return myPid();
        }


        @Override
        public void setIsSecureWrites(boolean sw)  {
            try {
                service.setIsSecureWrites(sw);
            } catch (RemoteException ignore) {

            }
        }

        @Override
        public long getModuleBaseAddr(String moduleName, int headType)  {
            try {
                return service.getModuleBaseAddr(moduleName,headType);
            } catch (RemoteException ignore) {
                return -1;
            }
        }

        @Override
        public long jump(long addr, int count)  {
            try {
                return service.jump(addr,count);
            } catch (RemoteException e) {
                return -1;
            }
        }

        @Override
        public long jump32(long addr)  {
            try {
                return service.jump32(addr);
            } catch (RemoteException e) {
                return -1;
            }
        }

        @Override
        public long jump64(long addr)  {
            try {
                return service.jump64(addr);
            } catch (RemoteException e) {
                return -1;
            }
        }

        @Override
        public int setMemoryAddrValue(String value, long addr, int type, boolean isFree)  {
            try {
                return service.setMemoryAddrValue(value,addr,type,isFree);
            } catch (RemoteException e) {
                return -1;
            }
        }

        @Override
        public String getMemoryAddrData(long addr, int type)  {
            try {
                return service.getMemoryAddrData(addr,type);
            } catch (RemoteException e) {
                return "";
            }
        }

        @Override
        public int getDword(long addr)  {
            try {
                return service.getDword(addr);
            } catch (RemoteException e) {
                return -1;
            }
        }

        @Override
        public float getFloat(long addr)  {
            try {
                return service.getFloat(addr);
            } catch (RemoteException e) {
                return -1;
            }
        }

        @Override
        public double getDouble(long addr)  {
            try {
                return service.getDouble(addr);
            } catch (RemoteException e) {
                return -1;
            }
        }

        @Override
        public long getQword(long addr)  {
            try {
                return service.getQword(addr);
            } catch (RemoteException e) {
                return -1;
            }
        }

        @Override
        public int getWord(long addr)  {
            try {
                return service.getWord(addr);
            } catch (RemoteException e) {
                return -1;
            }
        }

        @Override
        public byte getByte(long addr)  {
            try {
                return service.getByte(addr);
            } catch (RemoteException e) {
                return -1;
            }
        }

        @Override
        public String getString(long addr)  {
            try {
                return service.getString(addr);
            } catch (RemoteException e) {
                return "";
            }
        }

        @Override
        public int getDword_t(long addr)  {
            try {
                return service.getDword_t(addr);
            } catch (RemoteException e) {
                return -1;
            }
        }

        @Override
        public float getFloat_t(long addr)  {
            try {
                return service.getFloat_t(addr);
            } catch (RemoteException e) {
                return -1;
            }
        }

        @Override
        public double getDouble_t(long addr)  {
            try {
                return service.getDouble_t(addr);
            } catch (RemoteException e) {
                return -1;
            }
        }

        @Override
        public long getQword_t(long addr)  {
            try {
                return service.getQword_t(addr);
            } catch (RemoteException e) {
                return -1;
            }
        }

        @Override
        public int getWord_t(long addr)  {
            try {
                return service.getWord_t(addr);
            } catch (RemoteException e) {
                return -1;
            }
        }

        @Override
        public byte getByte_t(long addr)  {
            try {
                return service.getByte_t(addr);
            } catch (RemoteException e) {
                return -1;
            }
        }

        @Override
        public String getString_t(long startAddr)  {
            try {
                return service.getString_t(startAddr);
            } catch (RemoteException e) {
                return "";
            }
        }

        @Override
        public void setMemoryArea(int memoryArea)  {
            try {
                service.setMemoryArea(memoryArea);
            } catch (RemoteException ignore) {

            }
        }

        @Override
        public long[] MemorySearch(String value, int type)  {
            try {
                return service.MemorySearch(value,type);
            } catch (RemoteException e) {
                return new long[]{};
            }
        }

        @Override
        public long[] MemorySearchRange(String value, int type)  {
            try {
                return service.MemorySearchRange(value,type);
            } catch (RemoteException e) {
                return new long[]{};
            }
        }

        @Override
        public long[] MemorySearchUnited(String value, int type)  {
            try {
                return service.MemorySearchUnited(value,type);
            } catch (RemoteException e) {
                return new long[]{};
            }
        }

        @Override
        public long[] ImproveOffset(String value, int type, long offset)  {
            try {
                return service.ImproveOffset(value,type,offset);
            } catch (RemoteException e) {
                return new long[]{};
            }
        }

        @Override
        public long[] ImproveOffsetRange(String value, int type, long offset)  {
            try {
                return service.ImproveOffsetRange(value,type,offset);
            } catch (RemoteException e) {
                return new long[]{};
            }
        }

        @Override
        public long[] ImproveOffsetUnited(String value, int type, long offset)  {
            try {
                return service.ImproveOffsetUnited(value,type,offset);
            } catch (RemoteException e) {
                return new long[]{};
            }
        }

        @Override
        public long[] ImproveValue(String value, int type)  {
            try {
                return service.ImproveValue(value,type);
            } catch (RemoteException e) {
                return new long[]{};
            }
        }

        @Override
        public int MemoryOffsetWrite(String value, int type, long offset, boolean isFree)  {
            try {
                return service.MemoryOffsetWrite(value,type,offset,isFree);
            } catch (RemoteException e) {
                return -1;
            }
        }

        @Override
        public int getResultCount()  {
            try {
                return service.getResultCount();
            } catch (RemoteException e) {
                return -1;
            }
        }

        @Override
        public long[] getResultList()  {
            try {
                return service.getResultList();
            } catch (RemoteException e) {
                return new long[]{};
            }
        }

        @Override
        public int printResultListToFile(String filePath)  {
            try {
                return service.printResultListToFile(filePath);
            } catch (RemoteException e) {
                return -1;
            }
        }

        @Override
        public int clearResultList()  {
            try {
                return service.clearResultList();
            } catch (RemoteException e) {
                return -1;
            }
        }

        @Override
        public void setFreezeDelayMs(int delay)  {
            try {
                service.setFreezeDelayMs(delay);
            } catch (RemoteException ignore) {
            }
        }

        @Override
        public int getFreezeNum()  {
            try {
                return service.getFreezeNum();
            } catch (RemoteException e) {
                return -1;
            }
        }

        @Override
        public int addFreezeItem(String value, long addr, int type)  {
            try {
                return service.addFreezeItem(value,addr,type);
            } catch (RemoteException e) {
                return -1;
            }
        }

        @Override
        public int removeFreezeItem(long addr)  {
            try {
                return service.removeFreezeItem(addr);
            } catch (RemoteException e) {
                return -1;
            }
        }

        @Override
        public int removeAllFreezeItem()  {
            try {
                return service.removeAllFreezeItem();
            } catch (RemoteException e) {
                return -1;
            }
        }

        @Override
        public int startAllFreeze()  {
            try {
                return service.startAllFreeze();
            } catch (RemoteException e) {
                return -1;
            }
        }

        @Override
        public int stopAllFreeze()  {
            try {
                return service.stopAllFreeze();
            } catch (RemoteException e) {
                return -1;
            }
        }

        @Override
        public String getMemoryAddrMapLine(long address)  {
            try {
                return service.getMemoryAddrMapLine(address);
            } catch (RemoteException e) {
                return "";
            }
        }

        @Override
        public String getMapLineMemoryAreaName(String mapLine)  {
            try {
                return service.getMapLineMemoryAreaName(mapLine);
            } catch (RemoteException e) {
                return "";
            }
        }

        @Override
        public String getMemoryAreaIdName(int memid)  {
            try {
                return service.getMemoryAreaIdName(memid);
            } catch (RemoteException e) {
                return "";
            }
        }

        @Override
        public String getMemoryAreaName()  {
            try {
                return service.getMemoryAreaName();
            } catch (RemoteException e) {
                return "";
            }
        }

        @Override
        public String getDataTypeName(int typeId)  {
            try {
                return service.getDataTypeName(typeId);
            } catch (RemoteException e) {
                return "";
            }
        }
    };


    @Override
    public IBinder onBind(Intent intent) {
        return binder;
    }
}
